﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WcfSupportPlugin
{
    public partial class WcfWnd : WeifenLuo.WinFormsUI.Docking.DockContent
    {
        public WcfWnd()
        {
            InitializeComponent();
        }

        private void toolsEnableWcf_Click(object sender, EventArgs e)
        {
            try
            {
                Uri BAddr = new Uri(PluginObjects.WcfSupportPlugin.BaseAddress);
                PluginObjects.WcfHost = new ServiceHost(typeof(ScreenMonitor.ServerWcfService.ServerMainServuce), BAddr);
                WSHttpBinding binding = new WSHttpBinding();
                PluginObjects.WcfHost.AddServiceEndpoint(typeof(ScreenMonitor.ServerWcfService.ServerMainServuce),binding,"");
                PluginObjects.WcfHost.Open();
                MessageBox.Show("服务启动成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show("服务启动失败：" + ex.Message);
            }
        }

        private void toolsDisableWcf_Click(object sender, EventArgs e)
        {
            try
            {
                PluginObjects.WcfHost.Close();
                MessageBox.Show("服务关闭成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show("服务关闭失败：" + ex.Message);
            }
        }

        private void toolsWcfControl_DropDownOpening(object sender, EventArgs e)
        {
            if(PluginObjects.WcfHost==null || PluginObjects.WcfHost.State==CommunicationState.Created || PluginObjects.WcfHost.State==CommunicationState.Faulted || PluginObjects.WcfHost.State==CommunicationState.Closed)
            {
                toolsEnableWcf.Enabled = true;
                toolsDisableWcf.Enabled = false;
            }
            else
            {
                toolsEnableWcf.Enabled = false;
                toolsDisableWcf.Enabled = true;
            }
        }

    }
}
